package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.Constants;
import com.example.demo.controller.dto.UserDTO;
import com.example.demo.entity.Menu;
import com.example.demo.entity.User;
import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.RoleMenuMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IMenuService;
import com.example.demo.service.UserService;
import com.example.demo.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//@Service
//public interface UserService {
//
//    public List<User> findAll();
//
//    public int save(User user);
//
//
//    public int delete(Integer id);
//    public List<User> findPage(Integer pageNum,Integer pageSize,String username);
//    @Transactional
//    public Integer selectTotal(String username);
//}


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    private static final Log LOG = Log.get();

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuServiceImpl menuService;

    @Resource
    private RoleMenuMapper roleMenuMapper;
    public boolean saveUser(User user) {
     return saveOrUpdate(user);
    }

    public UserDTO login(UserDTO userDTO) {
        User one = getUserInfo(userDTO);
        if(one != null){
            BeanUtil.copyProperties(one,userDTO,true);
            String token = TokenUtils.genToken(one.getId().toString(),one.getPassword().toString());
            userDTO.setToken(token);
            String flag = one.getRole();

            userDTO.setMenus(getRoleMenus(flag));
            return userDTO;
        }else{
            throw new ServiceException(Constants.CODE_500,"用户名或者密码错误");
        }
    }
    public User register(UserDTO userDTO){
        User one = getUserInfo(userDTO);
        if(one == null){
            one = new User();
            BeanUtil.copyProperties(userDTO,one,true);
            save(one);
        }else{
            throw new ServiceException(Constants.CODE_500,"用户已存在");
        }
        return null;
    }
    private User getUserInfo(UserDTO userDTO){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        User one;
        try {
            one = getOne(queryWrapper);
        }catch (Exception e){
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500,"用户名或者密码错误");
        }
        return one;
    }
    public User getUser(String id){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User one;
        try {
            one = getOne(queryWrapper);
        }catch (Exception e){
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500,"用户名或者密码错误");
        }
        return one;
    }
    public boolean updateUserinfo(User user) {

        return updateById(user);
    }


    private List<Menu> getRoleMenus(String flag){
        Integer roleId =  roleMapper.selectByFlag(flag);
        // 当前角色的所有菜单id
        List<Integer> menuIds =  roleMenuMapper.selectByRoleId(roleId);
        // 查出系统所有菜单
        List<Menu> menus =   menuService.findMenus();
        List<Menu> roleMenus = new ArrayList<>();
        for (Menu menu : menus){
            if(menuIds.contains(menu.getId())){
                roleMenus.add(menu);
            }
            List<Menu> children =  menu.getChildren();
            children.removeIf(child->!menuIds.contains(child.getId()));
        }
        return roleMenus;
    }
}
